﻿using System;
using System.Text.RegularExpressions;
using System.Web.Configuration;

namespace AMing.Mvc4.Core.Extension
{
    public static class StringExtension
    {
        /// <summary>
        /// null改为空字符串
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string GetNotNull(this string str)
        {
            return str == null ? "" : str;
        }

        /// <summary>
        /// Json特殊字符过滤，参见http://www.json.org/
        /// </summary>
        /// <param name="sourceStr">要过滤的源字符串</param>
        /// <returns>返回过滤的字符串</returns>
        public static string JsonCharFilter(this string sourceStr)
        {
            sourceStr = sourceStr.Replace("\\", "\\\\");
            sourceStr = sourceStr.Replace("\b", "\\\b");
            sourceStr = sourceStr.Replace("\t", "\\\t");
            sourceStr = sourceStr.Replace("\n", "\\\n");
            sourceStr = sourceStr.Replace("\n", "\\\n");
            sourceStr = sourceStr.Replace("\f", "\\\f");
            sourceStr = sourceStr.Replace("\r", "\\\r");

            return sourceStr.Replace("\"", "\\\"");
        }

        /// <summary>
        /// 转成指定类型
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="val"></param>
        /// <param name="defaultval">转换失败的设置的值</param>
        /// <returns></returns>
        public static T To<T>(this string val, T defaultval = default(T)) where T : struct
        {
            try
            {
                if (string.IsNullOrWhiteSpace(val))
                {
                    return defaultval;
                }
                return (T)Convert.ChangeType(val, typeof(T), null);
            }
            catch (Exception)
            {
                return defaultval;
            }
        }

        /// <summary>
        /// 将指定字符串中的格式项替换为指定数组中相应对象的字符串表示形式。
        /// </summary>
        /// <param name="val"></param>
        /// <param name="args"></param>
        /// <returns></returns>
        public static string ToFromat(this string val, params object[] args)
        {
            return string.Format(val, args);
        }

        /// <summary>   
        /// 移除HTML标签   
        /// </summary>   
        /// <param name="HTMLStr">HTMLStr</param>   
        public static string RemoveHtmlTag(this string HTMLStr)
        {
            return System.Text.RegularExpressions.Regex.Replace(HTMLStr, "<[^>]*>", "");
        }
        /// <summary>
        /// 过滤脚本
        /// </summary>
        /// <param name="content"></param>
        /// <returns></returns>
        public static string FilterScript(this string content)
        {
            string regexstr = @"(?i)<script([^>])*>(\w|\W)*</script([^>])*>";
            return Regex.Replace(content, regexstr, string.Empty, RegexOptions.IgnoreCase);
        }

        /// <summary>
        ///地址前缀
        /// </summary>
        static string url_HostName = WebConfigurationManager.AppSettings["Url_HostName"];

        /// <summary>
        /// 追加前缀地址前缀
        /// </summary>
        public static string UrlAddHostName(this string url)
        {
#if RELEASE_HOSENAME
            return "{0}{1}".ToFromat(url_HostName, url);
#endif
            return url;
        }


        /// <summary>
        /// 还原搜索编码的关键字
        /// </summary>
        /// <param name="sourceStr"></param>
        /// <returns></returns>
        public static string ReplaceSearchKeyword(this string sourceStr)
        {
            sourceStr = sourceStr.Replace("[r1]", "~");
            sourceStr = sourceStr.Replace("[r2]", "!");
            sourceStr = sourceStr.Replace("[r3]", "@");
            sourceStr = sourceStr.Replace("[r4]", "#");
            sourceStr = sourceStr.Replace("[r5]", "$");
            sourceStr = sourceStr.Replace("[r6]", "%");
            sourceStr = sourceStr.Replace("[r7]", "^");
            sourceStr = sourceStr.Replace("[r8]", "&");
            sourceStr = sourceStr.Replace("[r9]", "*");
            sourceStr = sourceStr.Replace("[r10]", "{");
            sourceStr = sourceStr.Replace("[r11]", "}");
            sourceStr = sourceStr.Replace("[r12]", "+");
            sourceStr = sourceStr.Replace("[r13]", "?");
            sourceStr = sourceStr.Replace("[r14]", "=");
            sourceStr = sourceStr.Replace("[r15]", "_");
            sourceStr = sourceStr.Replace("[r16]", ".");
            sourceStr = sourceStr.Replace("[r17]", "-");

            return sourceStr;
        }
    }
}
